﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Utils;
using System.Configuration;

namespace DSAManager
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            loadConfig();
            AppContext.ProgressBar = progressBar1;
            AppContext.downbutton = btn_down;
            AppContext.sendbutton = btn_send;
        }

        #region 界面布局

        private FrmOrders frmOrders;
        private FrmChgPwd frmChgPwd;
        private FrmSetting frmSetting;
        private FrmPhotoSize frmPhotoSize;
        private FrmAdd frmAdd;
        private FrmSalesman frmSalesman;
        private FrmUser frmUser;


        void setBtnSatus(object currentBtn)
        {
            btn_orders.Checked = btn_orders == currentBtn;
            btn_refreshOrder.Checked = btn_refreshOrder == currentBtn;
            btn_setting.Checked = btn_setting == currentBtn;
            btn_photosize.Checked = btn_photosize == currentBtn;
            btn_changpassword.Checked = btn_changpassword == currentBtn;
            btn_add.Checked = btn_add == currentBtn;
        }


        private void btn_orders_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            openOrder();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            addForm(new FrmAdd());
        }



        void openOrder()
        {
            if (frmOrders == null)
                frmOrders = new FrmOrders();
            else
            {
                frmOrders.loadUnDownData(true);
            }
            addForm(frmOrders);
        }

        private void btn_refreshOrder_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmOrders == null)
                frmOrders = new FrmOrders();
            else
            {
                frmOrders.loadHasDownData(true);
            }
            addForm(frmOrders);
        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            if (frmOrders != null)
                frmOrders.download();
        }


        void addForm(Form form)
        {
            pan_forms.Controls.Clear();
            form.TopLevel = false;
            pan_forms.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            form.Show();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            openOrder();
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_photosize_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmPhotoSize == null)
                frmPhotoSize = new FrmPhotoSize();
            addForm(frmPhotoSize);
        }

        #endregion

        private void btn_changpassword_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmChgPwd == null)
                frmChgPwd = new FrmChgPwd();
            addForm(frmChgPwd);
        }

        private void btn_setting_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmSetting == null)
                frmSetting = new FrmSetting();
            addForm(frmSetting);
        }

        private void btn_grant_Click(object sender, EventArgs e)
        {
            Process.Start("iexplore.exe", "https://oauth.taobao.com/authorize?response_type=code&client_id=" + AppContext.client_id + "&redirect_uri=" + AppContext.redirect_uri);
        }

        void loadConfig()
        {
            BackgroundWorker bk = new BackgroundWorker();
            bk.DoWork += delegate
            {
                var result = Api.LoadConfigs();
                if (result.ok)
                {
                    foreach (var item in result.data)
                    {
                        if (item.ckey == "client_id") AppContext.client_id = item.cvalue;
                        if (item.ckey == "redirect_uri") AppContext.redirect_uri = item.cvalue;
                        if (item.ckey == "share_dir") AppContext.share_dir = item.cvalue;
                        if (item.ckey == "down_dir") AppContext.down_dir = item.cvalue;
                        if (item.ckey == "save_mode") AppContext.save_mode = IntExtension.Parse(item.cvalue);

                    }
                }
            };
            bk.RunWorkerAsync();
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            if (MsgBox.Ask("发送照片会清空打印服务器上的照片，是否继续?") == System.Windows.Forms.DialogResult.Yes)
            {
                if (frmOrders != null)
                    frmOrders.sendimages();
            }

        }

        private void btn_ditui_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmSalesman == null)
                frmSalesman = new FrmSalesman();
            addForm(frmSalesman);
        }

        private void btn_user_Click(object sender, EventArgs e)
        {
            setBtnSatus(sender);
            if (frmUser == null)
                frmUser = new FrmUser();
            addForm(frmUser);
        }

        private void btn_wdgrant_Click(object sender, EventArgs e)
        {
            var result = Api.GetWeidianToken(new Models.Input.WeidianTokenParaInfo()
            {
                AppId = ConfigurationManager.AppSettings["WeidianAppId"],
                AppSecret = ConfigurationManager.AppSettings["WeidianAppSecret"]
            });
            if (result.ok)
            {
                if (result.data.status.status_code == 0)
                {
                    MessageBox.Show("授权成功");
                }
                else
                    MessageBox.Show("授权失败,请重新授权");
            }
            else
                MessageBox.Show("授权失败,请重新授权");
        }
    }
}
